﻿// ***********************************************************************
// Assembly         : Admin.Framework.Core.Infrastructure
// Author           : xwltz
// Created          : 2019-05-30
//
// Last Modified By : xwltz
// Last Modified On : 2019-05-30
// ***********************************************************************
// <summary>
//  日志帮助类
// </summary>
// ***********************************************************************

using System;
using log4net;

namespace Admin.Framework.Core.Infrastructure.Helper
{
    public static class LogHelper
    {
        public static object LogLock = new object();

        public static ILog GetLogger(string name)
        {
            lock (LogLock)
            {
                return LogManager.GetLogger(FrameworkConfigs.LogRepository, name);
            }
        }

        public static readonly ILog LogInfo  = GetLogger("LogInfo");
        public static readonly ILog LogError = GetLogger("LogError");
        public static readonly ILog LogMySql = GetLogger("LogMySql");
        public static readonly ILog LogMsSql = GetLogger("LogMsSql");

        #region HTML

        /// <summary>
        /// 普通日志 Html版本
        /// </summary>
        /// <param name="info"></param>
        public static void WriteHtml(string info)
        {
            
            if (!LogInfo.IsInfoEnabled) return;
            LogInfo.Info(info);
            LogMySql.Info(info);
            LogMsSql.Info(info);
        }

        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="info">附加信息</param>
        /// <param name="ex">错误</param>
        public static void ErrorHtml(string info, Exception ex)
        {
            if (!string.IsNullOrEmpty(info) && ex == null)
            {
                LogInfo.InfoFormat("【附加信息】：{0}<br>", new object[] { info });
                LogMySql.ErrorFormat("【附加信息】：{0}<br>", new object[] { info });
                LogMsSql.ErrorFormat("【附加信息】：{0}<br>", new object[] { info });
            }
            else if (!string.IsNullOrEmpty(info) && ex != null)
            {
                var errorMsg = BeautyErrorHtml(ex);
                LogError.ErrorFormat("【附加信息】：{0}<br>{1}", new object[] { info, errorMsg });
                LogMySql.ErrorFormat("【附加信息】：{0}<br>{1}", new object[] { info, errorMsg });
                LogMsSql.ErrorFormat("【附加信息】：{0}<br>{1}", new object[] { info, errorMsg });
            }
            else if (string.IsNullOrEmpty(info) && ex != null)
            {
                var errorMsg = BeautyErrorHtml(ex);
                LogError.Error(errorMsg);
                LogMySql.Error(errorMsg);
                LogMsSql.Error(errorMsg);
            }
        }

        /// <summary>
        /// 美化错误信息
        /// </summary>
        /// <param name="ex">异常</param>
        /// <returns>错误信息</returns>
        private static string BeautyErrorHtml(Exception ex)
        {
            var errorMsg = string.Format("【异常类型】：{0} <br>【异常信息】：{1} <br>【堆栈调用】：{2}", new object[] { ex.GetType().Name, ex.Message, ex.StackTrace });
            errorMsg = errorMsg.Replace("\r\n", "<br>");
            errorMsg = errorMsg.Replace(" ", "");
            errorMsg = errorMsg.Replace("位置", "<strong style=\"color:red\">位置</strong>");
            return errorMsg;
        }

        #endregion

        #region TXT

        /// <summary>
        /// 普通日志 Txt版本
        /// </summary>
        /// <param name="info"></param>
        public static void WriteTxt(string info)
        {
            if (!LogInfo.IsInfoEnabled) return;
            LogInfo.Info(info);
            LogMySql.Info(info);
            LogMsSql.Info(info);
        }

        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="info">附加信息</param>
        /// <param name="ex">错误</param>
        public static void ErrorTxt(string info, Exception ex)
        {
            if (!string.IsNullOrEmpty(info) && ex == null)
            {
                LogInfo.InfoFormat("【附加信息】：{0}\r\n", new object[] { info });
                LogMySql.ErrorFormat("【附加信息】：{0}\r\n", new object[] { info });
                LogMsSql.ErrorFormat("【附加信息】：{0}\r\n", new object[] { info });
            }
            else if (!string.IsNullOrEmpty(info) && ex != null)
            {
                var errorMsg = BeautyErrorTxt(ex);
                LogError.ErrorFormat("【附加信息】：{0}\r\n{1}", new object[] { info, errorMsg });
                LogMySql.ErrorFormat("【附加信息】：{0}\r\n{1}", new object[] { info, errorMsg });
                LogMsSql.ErrorFormat("【附加信息】：{0}\r\n{1}", new object[] { info, errorMsg });
            }
            else if (string.IsNullOrEmpty(info) && ex != null)
            {
                var errorMsg = BeautyErrorTxt(ex);
                LogError.Error(errorMsg);
                LogMySql.Error(errorMsg);
                LogMsSql.Error(errorMsg);
            }
        }

        /// <summary>
        /// 美化错误信息
        /// </summary>
        /// <param name="ex">异常</param>
        /// <returns>错误信息</returns>
        private static string BeautyErrorTxt(Exception ex)
        {
            var errorMsg = string.Format("【异常类型】：{0} \r\n【异常信息】：{1} \r\n【堆栈调用】：{2}", new object[] { ex.GetType().Name, ex.Message, ex.StackTrace });
            //errorMsg = errorMsg.Replace("\r\n", "%n");
            //errorMsg = errorMsg.Replace(" ", "");
            //errorMsg = errorMsg.Replace("位置", "<strong style=\"color:red\">位置</strong>");
            return errorMsg;
        }

        #endregion
    }
}